import java.util.Random;
import java.util.concurrent.*;

public class ThreadLocalRandomTest {
    public static void main(String[] args) throws InterruptedException {
        long start = System.currentTimeMillis();
        Random random = new Random();
        CountDownLatch countDownLatch = new CountDownLatch(100);
        CyclicBarrier bar = new CyclicBarrier(100);
        ExecutorService exec = Executors.newFixedThreadPool(100);
        for (int i = 0; i < 100; i++) {
            exec.submit(new RandomDemo0Runner(bar,"thread"+i,random,countDownLatch));
        }
        countDownLatch.await();
        long use = System.currentTimeMillis() - start;
        System.out.println(" main is over..." + use);
        exec.shutdown();
    }
}

class RandomDemo0Runner implements Runnable {
    private CyclicBarrier barrier;
    private String name;
    private Random random;
    private CountDownLatch cd;

    RandomDemo0Runner(CyclicBarrier barrier, String name, Random random, CountDownLatch cd) {
        this.barrier = barrier;
        this.name = name;
        this.random = random;
        this.cd = cd;
    }

    @Override
    public void run() {
        try {
            System.out.println(name + "准备好了...");
            barrier.await();
            for (int i = 0; i < 10000; i++) {
                this.random.nextInt(50);
//                Random random = new Random();
//                random.nextInt(50);
            }
            cd.countDown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }

    }


}
